<%@page import="com.jm.web.form.Button"%>
<%@page import="com.jm.web.form.Toolbar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/wfpagebackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	StringBuilderEx sql = null;
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		sql = new StringBuilderEx();
	}
	
	protected void EndProcess() throws Exception{
			if("save".equalsIgnoreCase(form.getAction()) 
			|| "delete".equalsIgnoreCase(form.getAction())
			|| "wfcheck".equalsIgnoreCase(form.getAction())
			|| "wfstart".equalsIgnoreCase(form.getAction())
			|| "wfcancel".equalsIgnoreCase(form.getAction())){
			onSearchRetrieveResult();
			if(!ret.isError())
				utils.getGlobalHelper().reloadSubProMap();
		}
	}
	
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		if(!form.isNullOrEmpty("applysn")){
			rs = utils.executeQuery("info5","select *,hr003name as hr003 from pan..pan_SUBPRO_APPIP where sn = '"+form.gpsv("applysn")+"'");
			if(rs.next()){
				form.FromResultSet(rs, false);
			}else{
				ret.setWarningInfo("此單已刪除,如需要請重新新增!");
				page.getToolbar().disableAllButtonExcept("newnext");
			}
			onSearchRetrieveResult();
		}else{
			form.spsv("ip", utils.getRemoteAddr());
		}
		return true ;
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{
		if(!form.isNullOrEmpty("applysn")){
			rs = utils.executeQuery("info5","select * from pan.dbo.PAN_SUBPRO_APPHRNUM where applysn = '"+form.gpsv("applysn")+"'");
			ret.SetSearchResult(rs);
		}
	}
	
	protected void doSave() throws Exception{
		if(form.getActionValue().length() == 0){
			//1.儲存電腦基本信息
			CrudDataEntity crud = form.getCrudDataEntity("info5","pan..PAN_SUBPRO_APPIP","sn");
			crud.spsv("sn", form.gpsv("applysn"));
			crud.setSpportSimple(true);
			crud.spsv("hr003atsn", form.gpsv("hr003atsn"));
			crud.spsv("hr003aname", form.gpsv("hr003atsn_text"));
			crud.spsv("hr003tsn", form.gpsv("hr003tsn"));
			crud.spsv("hr003name", form.gpsv("hr003"));
			crud.spsv("applyhrnum",form.gpsv("applyhrnum"));
			crud.spsv("applychname",form.gpsv("applychname"));
			crud.spsv("IP",form.gpsv("IP"));
			
			//驗證工號,部門,公司別是否合法
			rs = utils.executeQuery("info5",StringHelper.Format("select count(*) as cnt from hr..hr005 a where a.hrnum <> '' and a.leaveday ='' and a.hrnum = '%1$s' and a.fhr003tsn = '%2$s' and a.fhr003atsn = '%3$s'"
					,crud.gpsv("applyhrnum")
					,crud.gpsv("hr003tsn")
					,crud.gpsv("hr003atsn")
				));
			if(!rs.next() || rs.getInt("cnt") == 0){
				ret.setErrorInfo("工號,部門,公司別不合法");
				return ;
			}
			if(crud.isInsert()){
				crud.setGetLastKey(true);
				crud.appendCols("prgname,crtDay,crtIP");
				crud.insert();
			}else{
				crud.appendCols("updDay,updIP");
				crud.update();
			}
			
			if(form.isNullOrEmpty("applysn")){//設定主鍵值
				form.spsv("applysn", crud.gpsv("sn"));
				form.spsv("wfstate", "0");//流程中用到
			}
			ret.setOkInfo("儲存成功!");
		}
		
		//2.儲存可使用此臺電腦的人員明細
		if(form.getActionValue().length() == 0 || form.ationValueEq("saverow")){
			if(!form.isNullOrEmpty("applysn")){
				BaseDataEntities records = form.getrecords("grid1_records");
				for (int i=0;i<records.size();i++){
					BaseDataEntity record = (BaseDataEntity)records.get(i);
					CrudDataEntity apphrnum = record.getCrudDataEntity("info5","pan..PAN_SUBPRO_APPHRNUM","sn");
					apphrnum.spsv("applysn", form.gpsv("applysn"));
					apphrnum.spsv("hrnum", record.gpsv("hrnum"));
					
					CrudDataEntity temp = record.getCrudDataEntity("info5","pan..PAN_SUBPRO_APPHRNUM","sn");
					apphrnum.CopyTo(temp,"applysn,hrnum",false);
					temp.setUtils(utils);
					if(temp.isNullOrEmpty("hrnum") || temp.isExist("applysn,hrnum")){
						continue;
					}
					
					apphrnum.spsv("chname", record.gpsv("chname"));
					apphrnum.setSpportSimple(true);
					apphrnum.setUtils(utils);
					apphrnum.appendSaveCols();
					apphrnum.save();
				};
			}
			ret.setOkInfo("儲存成功!");
		}
		
		if(form.ationValueEq("startrow")){
			sql.Reset();
			String sns[] = form.gpsvs("key");
			if(sns.length>0){
				sql.Append("update pan..PAN_SUBPRO_APPHRNUM set status = 1 where applysn = '%1$s' and status = 0 and sn in(%2$s)"
					,form.gpsv("applysn")
					,StringHelper.arrayToString(sns,"'",","));
				utils.executeUpdate("info5",sql.toString());
				ret.setOkInfo("送簽成功!");
				
				//郵件通知
				Mail mail = new Mail();
				mail.setEncode(false);
				mail.setSubject("[SubPro]系統權限 單據號:"+this.getWFBillNo()+" 簽核提醒!");
				mail.setTo("yanjie@mail.jmknit.com,dena@mail.jmknit.com,helen@mail.jmknit.com");
				utils.sendMail(mail);
			}
		}
		
	}
	
	protected void doDelete() throws Exception{
		if(form.getActionValue().length() == 0 && !form.isNullOrEmpty("applysn")){
			//1.驗證是否有子目錄
			rs = utils.executeQuery("info5", "select count(*) as cnt from pan..PAN_SUBPRO_APPHRNUM where applysn = '"+form.gpsv("applysn")+"'");
			if(rs.next() && rs.getInt("cnt") > 0){
				ret.setErrorInfo("請先刪使用人員");
				return ;
			}
			
			utils.executeUpdate("info5", "delete from pan..PAN_SUBPRO_APPIP where sn = '"+form.gpsv("applysn")+"'");
			ret.setOkInfo("刪除成功!");
		}
		
		if("deleteRow".equals(form.getActionValue())){
			sql.Reset();
			String sns[] = form.gpsvs("key");
			int icode = 0; 
			if(sns.length>0){
				sql.Append(" delete a from pan..PAN_SUBPRO_APPHRNUM a"
				+" where a.sn in(%1$s) ",StringHelper.arrayToString(sns,"'",","));
				icode = utils.executeUpdate("info5",sql.toString());
			}
			if(icode > 0){
				ret.setOkInfo("刪除成功!");
			}else{
				ret.setErrorInfo("刪除失敗!");
			}
		}
	}
	
	//取單據號
	public String getWFBillNo(){
		return form.gpsv("hr003")+"["+form.gpsv("ip")+"]";
	}
	
	//取工作流
	public WorkFlow getWorkFlow(){
		return utils.getGlobalHelper().getWorkFlow("info5", "[SubPro]系統權限");
	}
	
	//送簽按鈕權限
	protected boolean isDisableWFStart() throws Exception{
		return false;
	}
	
	//取消流程按鈕權限
	protected boolean isDisableWFCancel() throws Exception{
		return false;
	}
	
	public String getWFDataKeyValue(){
		return form.gpsv("applysn");
	}
	
	public void EndStartWF() throws Exception{
		//更新status
		utils.executeUpdate("info5",StringHelper.Format("update pan..PAN_SUBPRO_APPHRNUM set status = 1 where applysn = '%1$s' and status = 0",form.gpsv("applysn")));
	}
		
	public void ProcessWFStartPageButton(Toolbar toolbar, List defaultItems) throws Exception{
		super.ProcessWFStartPageButton(toolbar,defaultItems);
		if(defaultItems == null)return;
		Button button = null;
		button = this.getButton("delete",defaultItems);
		if(button !=null)button.setDisabled(false);
		
		button = this.getButton("save",defaultItems);
		if(button !=null)button.setDisabled(false);
	}
	
	protected boolean onCustomAction(String action) throws Exception{
		if("getHrnum".equalsIgnoreCase(action)){
			return getHrnum();
		}
		return super.onCustomAction(action);
	}
	
	
	protected boolean getHrnum() throws Exception{
		PageResult pageResult = ret.getCallResult();
		String hrnum = form.gpsv("hrnum");
		
		if(StringHelper.IsNullOrEmpty(hrnum)){
			pageResult.setRetCode(Errors.NOK);
		}else{
			rs = utils.executeQuery("info5",StringHelper.Format("select hrnum ,chname from hr..hr005 where leaveday = '' and hrnum = '%1$s'",hrnum));
			if(rs.next()){
				pageResult.getData().FromResultSet(rs, false);
			}
		}
		this.getPage().Output(pageResult.ToJSONString());
		return true;
	}

}
%>
<%@include file="/include/common/wfpagebackend.jsp"%>
